@extends('admin.layout.main')
@section("content")
    <div class="layui-tab layui-tab-brief" lay-filter="demoTitle">
        <ul class="layui-tab-title site-demo-title">
            <li class="layui-this"><a href="javascript:location.reload()">文章审核管理</a></li>
        </ul>
        <div class="layui-body layui-tab-content site-demo site-demo-body">
            <div class="layui-tab-item layui-show">
                <div class="layui-main">
                    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
                        <legend>文章审核管理</legend>
                    </fieldset>
                    <table class="layui-table">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>标题</th>
                            <th>创建时间</th>
                            <th>修改时间</th>
                            <th width="130">操作</th>
                        </tr>
                        </thead>
                        @foreach($posts as $post)
                            <tr>
                                <td>{{$post->id}}</td>
                                <td>{{$post->title}}</td>
                                <td>{{$post->created_at}}</td>
                                <td>{{$post->updated_at}}</td>
                                <td>
                                    <a class="layui-btn layui-btn-lg post-audit" post-id="{{$post->id}}"
                                       post-action-status="1"
                                       href="javasctipt:;">通过审核</a>
                                    <br/><br/>
                                    <a class="layui-btn layui-btn-lg layui-btn-danger post-audit"
                                       post-id="{{$post->id}}"
                                       post-action-status="-1"
                                       href="javasctipt:;">不通过审核</a>
                                </td>
                            </tr>
                        @endforeach
                    </table>
                    {{$posts->links()}}
                </div>
            </div>
        </div>
    </div>
    <script>
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $(".post-audit").click(function (event) {
            target = $(event.target);
            var post_id = target.attr("post-id");
            var status = target.attr("post-action-status");
            $.ajax({
                url: "/admin/posts/" + post_id + "/status",
                method: "POST",
                data: {
                    "status": status,
                },
                dataType: "json",
                success: function(data) {
                    if (data.error != 0) {
                        alert(data.msg);
                        return;
                    }

                    target.parent().parent().remove();
                },
                error:function(){
                    alert('请求失败');
                }

            });
        });


    </script>
@endsection
